package cz.proks.schoolpupilbook.shared.domain;

import java.util.LinkedList;

public class User implements IsIndexableDomain, Comparable<User> {
	public enum Role { 
		STUDENT, PARENT, LECTOR;
		
		public static LinkedList<Role> asList() {
			LinkedList<Role> roles = new LinkedList();			
			Role[] values = values();			
			
			for (Role role : values)
				roles.add(role);
			
			return roles;
		}
		
		public static LinkedList<String> asStringList() {
			LinkedList<String> roles = new LinkedList();			
			Role[] values = values();			
			
			for (Role role : values)
				roles.add(role.toString());
			
			return roles;
		}
	};
		
	private String id;
	private String name;
	private String surname;
	private Role role;
	private String username;
	private String password;
	
	public User() {	
	}	
	
	public User(String id, String name, String surname, Role role, String username, String password) {
		super();
		this.id = id;
		this.name = name;
		this.surname = surname;
		this.role = role;
		this.username = username;
		this.password = password;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}
		
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFullName() {		
		StringBuilder sb = new StringBuilder();
		
		if (getName() != null)
			sb.append(getName());
		
		if (getSurname() != null)
			sb.append(sb.length() > 0 ? " " : "").append(getSurname());
		
		return sb.length() > 0 ? sb.toString() : null;
	}		
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		return true;
	}

	@Override
	public int compareTo(User o) {
		return o == null ? 1 : getFullName().compareToIgnoreCase(o.getFullName());
	}		
}
